{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "#### 1.导入模块\n",
    "\n",
    "import requests\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "#### 2.输入face++官网给的api_secret和\n",
    "\n",
    "api_keyapi_secret = \"P6EBN3oTiWF7aUTXWfKtUkej449P8hJa\"\n",
    "api_key = '1SHS4EBFoKgvOGf_UhuhrMA3MlGBQ2iT'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#### 3.detect face 添加一张人脸的图片，检测人脸\n",
    "\n",
    "api_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "img_url = \"https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1015770032,4028183792&fm=26&gp=0.jpg\"\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "}\n",
    "payload = {\n",
    "    \"image_url\":img_url,\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'return_attributes':'gender,age,smiling,emotion', \n",
    "}\n",
    "r = requests.post(api_url, params=payload, headers=headers)\n",
    "r.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1603538037,d364261f-e947-4b18-a2a4-e2da2500e985',\n",
       " 'time_used': 152,\n",
       " 'faces': [{'face_token': '93fb0996c30e7d8f7c753c97294da7ec',\n",
       "   'face_rectangle': {'top': 98, 'left': 159, 'width': 75, 'height': 75},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 24},\n",
       "    'smile': {'value': 2.462, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 3.131,\n",
       "     'disgust': 0.123,\n",
       "     'fear': 0.849,\n",
       "     'happiness': 3.828,\n",
       "     'neutral': 83.043,\n",
       "     'sadness': 7.063,\n",
       "     'surprise': 1.963}}}],\n",
       " 'image_id': '2j2ZkkWIdaitgpg8grqiPw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#返回人脸信息\n",
    "\n",
    "results = r.json()\n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '3b5a6b8c16d13d71ad22823da5a734e8',\n",
       " 'time_used': 145,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1603538137,70bced4a-6812-4aab-b82b-2d8b71dfadc6',\n",
       " 'outer_id': 'ysx',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#### 4.compare face 创建一个人脸的集合，存储人脸标识face_token一个FaceSet能够存储10000个face_token。\n",
    "\n",
    "api_secret = \"P6EBN3oTiWF7aUTXWfKtUkej449P8hJa\"\n",
    "api_key = '1SHS4EBFoKgvOGf_UhuhrMA3MlGBQ2iT'\n",
    "display_name = \"相册\" # 人脸集合的名字\n",
    "outer_id = \"ysx\" # 账号下全局唯一的FaceSet自定义标识，可以用来管理 FaceSet 对象。\n",
    "api_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "payload = {\n",
    "    # 请求参数\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'display_name':display_name,\n",
    "    'outer_id':outer_id,\n",
    "}\n",
    "r = requests.post(api_url, params=payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '3b5a6b8c16d13d71ad22823da5a734e8',\n",
       " 'tags': '',\n",
       " 'time_used': 117,\n",
       " 'user_data': '',\n",
       " 'display_name': '相册',\n",
       " 'face_tokens': [],\n",
       " 'face_count': 0,\n",
       " 'request_id': '1603538155,8a2838f6-214c-4e91-b8a5-0ec289acc539',\n",
       " 'outer_id': 'ysx'}"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#### 5.FaceSet GetDetail 获取一个FaceSet的所有信息，包括此FaceSet的faceset_token,outer_id,display_name的信息，以及此FaceSet中存放的face_token数量与列表\n",
    "\n",
    "api_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'outer_id':outer_id,\n",
    "}\n",
    "r = requests.post(api_url,params=payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '3b5a6b8c16d13d71ad22823da5a734e8',\n",
       " 'time_used': 520,\n",
       " 'face_count': 1,\n",
       " 'face_added': 1,\n",
       " 'request_id': '1603538316,95e70f2a-93f5-4cf9-8912-cb50554939e6',\n",
       " 'outer_id': 'ysx',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#### 6.FaceSet AddFace 为已经创建好的FaceSet添加人脸标识face_token.\n",
    "\n",
    "api_url = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/addface\"\n",
    "\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'faceset_token':'3b5a6b8c16d13d71ad22823da5a734e8',\n",
    "    'face_tokens':'93fb0996c30e7d8f7c753c97294da7ec',\n",
    "}\n",
    "r = requests.post(api_url,params=payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '3b5a6b8c16d13d71ad22823da5a734e8',\n",
       " 'face_removed': 1,\n",
       " 'time_used': 158,\n",
       " 'face_count': 0,\n",
       " 'request_id': '1603538382,6ed7cf8b-547e-47b6-8694-53bc070d6f86',\n",
       " 'outer_id': 'ysx',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#### 7.FaceSet RemoveFace 移除一个FaceSet中的某些或者全部face_token\n",
    "\n",
    "api_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/removeface\"\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'faceset_token':'3b5a6b8c16d13d71ad22823da5a734e8',\n",
    "    'face_tokens':'93fb0996c30e7d8f7c753c97294da7ec',\n",
    "}\n",
    "r = requests.post(api_url,params=payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '3b5a6b8c16d13d71ad22823da5a734e8',\n",
       " 'request_id': '1603538404,93171db6-df01-4449-a1be-e3150c721e63',\n",
       " 'time_used': 72,\n",
       " 'outer_id': 'ysx'}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#### 8.FaceSet Update 更新人脸集合的属性\n",
    "\n",
    "api_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/update\"\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'faceset_token':'3b5a6b8c16d13d71ad22823da5a734e8',\n",
    "    'display_name':\"相册\",\n",
    "}\n",
    "r = requests.post(api_url,params=payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#### 9.1检测两张需要对比的人脸，获得face_token\n",
    "\n",
    "api_secret = \"P6EBN3oTiWF7aUTXWfKtUkej449P8hJa\"\n",
    "api_key = '1SHS4EBFoKgvOGf_UhuhrMA3MlGBQ2iT'\n",
    "api_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "zxt = \"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603548595862&di=37e4aa14006d7ad9e22a1ece09194168&imgtype=0&src=http%3A%2F%2Finews.gtimg.com%2Fnewsapp_ls%2F0%2F11603382455_640330%2F0.jpg\"\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "}\n",
    "payload = {\n",
    "    \"image_url\":zxt,\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'return_attributes':'gender,age,smiling,emotion', \n",
    "}\n",
    "r = requests.post(api_url, params=payload, headers=headers)\n",
    "r.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1603538583,275090de-573a-4e03-8164-c5ab1160741e',\n",
       " 'time_used': 283,\n",
       " 'faces': [{'face_token': '562441a826e0f31b6045d73682591806',\n",
       "   'face_rectangle': {'top': 110, 'left': 243, 'width': 125, 'height': 125},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 24},\n",
       "    'smile': {'value': 0.0, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.015,\n",
       "     'disgust': 0.015,\n",
       "     'fear': 0.015,\n",
       "     'happiness': 0.117,\n",
       "     'neutral': 89.727,\n",
       "     'sadness': 0.89,\n",
       "     'surprise': 9.221}}}],\n",
       " 'image_id': 'GyDHIryA41dvfIZutO/W4Q==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results = r.json()\n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#### 9.2 第二张人脸图片\n",
    "\n",
    "api_secret = \"P6EBN3oTiWF7aUTXWfKtUkej449P8hJa\"\n",
    "api_key = '1SHS4EBFoKgvOGf_UhuhrMA3MlGBQ2iT'\n",
    "api_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "ysx = \"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603548102690&di=d033cf2aaea436ca7694b8f4e84e8f31&imgtype=0&src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F201912%2F18%2F20191218194722_gakqk.thumb.400_0.jpg\"\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "}\n",
    "payload = {\n",
    "    \"image_url\":ysx,\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'return_attributes':'gender,age,smiling,emotion', \n",
    "}\n",
    "r = requests.post(api_url, params=payload, headers=headers)\n",
    "r.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1603538676,8a10f653-619d-49c8-b05d-1811d3f64b7b',\n",
       " 'time_used': 241,\n",
       " 'faces': [{'face_token': 'e1900ea471e435f59c1b4514249b77e2',\n",
       "   'face_rectangle': {'top': 98, 'left': 159, 'width': 75, 'height': 75},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 26},\n",
       "    'smile': {'value': 1.054, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 7.802,\n",
       "     'disgust': 0.119,\n",
       "     'fear': 0.731,\n",
       "     'happiness': 5.801,\n",
       "     'neutral': 77.106,\n",
       "     'sadness': 3.717,\n",
       "     'surprise': 4.722}}}],\n",
       " 'image_id': 'SkimIvFZcbiT4SW+kR5J6Q==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results = r.json()\n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'confidence': 64.31,\n",
       " 'request_id': '1603538853,fa8482c1-a41a-4550-93a8-52b08696e651',\n",
       " 'time_used': 548,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-5': 73.975, '1e-4': 69.101}}"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#### 10.Compare Face 将两个人脸进行比对，来判断是否为同一个人，返回比对结果置信度和不同误识率下的阈值.\n",
    "api_secret = \"P6EBN3oTiWF7aUTXWfKtUkej449P8hJa\"\n",
    "api_key = '1SHS4EBFoKgvOGf_UhuhrMA3MlGBQ2iT'\n",
    "api_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "ysx = 'e1900ea471e435f59c1b4514249b77e2'\n",
    "zxt = '562441a826e0f31b6045d73682591806'\n",
    "             # 第二张人脸图片的url\n",
    "payload ={\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'face_token1': ysx,\n",
    "    'face_token2': zxt\n",
    "}\n",
    "r = requests.post(api_url,params=payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6rc1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
